package com.keyue.controller;

import com.alibaba.fastjson.JSONObject;
import com.keyue.common.annotation.AnonymousAccess;
import com.keyue.common.constant.Constant;
import com.keyue.common.constant.ServerResult;
import com.keyue.common.jsr303.OperateGroup;
import com.keyue.common.utils.RedisUtil;
import com.keyue.common.utils.RequestStringUtils;
import com.keyue.common.utils.StringUtils;
import com.keyue.domain.vo.AddressInfoQueryVO;
import com.keyue.domain.vo.EquipmentInfoQueryVO;
import com.keyue.domain.vo.HomeImageReqVO;
import com.keyue.service.AddressInfoService;
import com.keyue.service.EquipmentInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 设备信息(EquipmentInfo)表控制层
 *
 * @author makejava
 * @since 2025-07-04 15:20:03
 */
@RestController
@RequestMapping("/keyue/admin/equipment/open")
public class EquipmentOpenController {
    /**
     * 服务对象
     */
    @Resource
    private EquipmentInfoService equipmentInfoService;

    @Resource
    private AddressInfoService addressInfoService;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 分页查询
     *
     * @param equipmentInfoQueryVO 查询条件
     * @return 查询结果
     */
    @AnonymousAccess
    @PostMapping("/list_by_page")
    public ServerResult listByPage(@RequestBody @Validated({OperateGroup.List.class}) EquipmentInfoQueryVO equipmentInfoQueryVO) {
        return equipmentInfoService.listByPage(equipmentInfoQueryVO);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param equipmentInfoQueryVO 查询条件
     * @return 单条数据
     */
    @AnonymousAccess
    @PostMapping("/query_by_id")
    public ServerResult queryById(@RequestBody EquipmentInfoQueryVO equipmentInfoQueryVO) {
        return equipmentInfoService.queryById(equipmentInfoQueryVO);
    }

    /**
     * 根据IP获取城市地址
     *
     * @param request 查询条件
     * @return 单条数据
     */
    @AnonymousAccess
    @PostMapping("/get_address_by_ip")
    public ServerResult getAddressByIp(HttpServletRequest request) {
        return ServerResult.success(RequestStringUtils.getCityBy(request));
    }

    /**
     * 省市区下拉列表
     *
     * @param request 查询条件
     * @return 单条数据
     */
    @AnonymousAccess
    @PostMapping("/query_address")
    public ServerResult queryAddress(HttpServletRequest request) {
        return addressInfoService.queryAddress();
    }

    /**
     * 获取代理信息
     *
     * @param queryVO 查询条件
     * @return 单条数据
     */
    @AnonymousAccess
    @PostMapping("/get_counsellor_by")
    public ServerResult getCounsellorBy(@RequestBody AddressInfoQueryVO queryVO) {
        return addressInfoService.getCounsellorBy(queryVO);
    }

    /**
     * 首页图
     *
     * @param reqVO 查询条件
     * @return 查询结果
     */
    @AnonymousAccess
    @PostMapping("/home_picture_list")
    public ServerResult homePictureList(@RequestBody HomeImageReqVO reqVO) {
        String str = redisUtil.get(Constant.HOME_PICTURE_KEY);
        if (StringUtils.isBlank(str)) {
            return ServerResult.success();
        }
        return ServerResult.success(JSONObject.parseObject(str, HomeImageReqVO.class));
    }

}

